<?php
    session_start();
    require_once 'php/database.php';
    require_once 'php/photo.php';

	if (isset($_SESSION['username'])){
			$login = true;
	}
	else{
			$login = false;
	}
    function curPageURL() {
         $pageURL = 'http';
         if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
         $pageURL .= "://";
         if ($_SERVER["SERVER_PORT"] != "80") {
          $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
         } else {
          $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
         }
         return $pageURL;
    }

    function getUserNamePage(){
        if (isset($_SESSION['username']))
	{
		$login = true;
	}
	else{
		$login = false;
		//header("Location: ".$CFG->url."home.php");
	}
	$loguname = $_SESSION['username'];
	$username = $_GET["un"];
	if ($username==NULL&&$login){
		$username=$loguname;
	}
        return $username;
    }
?>

<?php
                echo '<table>';
                    echo '<tr>';
                        echo '<td valign="middle">';
                            
                                $user = getUserNamePage();
                                $hasil = database::query("SELECT * FROM user WHERE Username = '$user'");
                                if (mysql_num_rows($hasil)>0){
                                    $baris = mysql_fetch_row($hasil);
                                    $nama = $baris[3];
                                    $email = $baris[4];
                                    $tanggal = $baris[5];
                                    $gambar = photo::getProfPicPath($user);

                                    echo '<table>';
                                        echo '<tr>';
                                            echo '<td>';
                                                echo '<div id="fotopropic" class="boxy">';
                                                    echo '<img src="'.$gambar.'" alt="Foto Profil disini" id="foto_profil" name="foto_profil" width="100" height="100"/>';
                                                echo '</div>';
                                            echo '</td>';
                                        echo '</tr>';
                                        echo '<tr>';
                                            echo '<td>';
                                                echo '<br/>';
                                                echo '<div>';
                                                    echo '<label style="margin-left:20px; text-align:center;" id="nama">'.$nama.'</label>';
                                                echo '</div>';
                                                echo '<div>';
                                                    echo '<label style="margin-left:20px; text-align:center;" id="email">'.$email.'</label>';
                                                echo '</div>';
                                                echo '<div>';
                                                    echo '<label style="margin-left:20px; text-align:center;" id="tanggal">'.$tanggal.'</label>';
                                                echo '</div>';
                                            echo '</td>';
                                        echo '</tr>';
                                    echo '</table>';

                                }
                            
                        echo '</td>';
                        echo '<td>';
                            echo '<p align="left" style="vertical-align: top;">Following</p>';
                            
                            $user = getUserNamePage();

                            $hasil = database::query("SELECT Username FROM follownet WHERE Follower = '$user'");
                            $indeks = 0;
                            $count = 0;
                            if(mysql_num_rows($hasil)>0){
                                while($baris = mysql_fetch_row($hasil)){

                                    $pemilik = $baris[0];
                                    $gambar = photo::getProfPicPath($pemilik);
                                    $pemilikArray3[$indeks] = $pemilik;
                                    $gambarArray3[$indeks] = $gambar;
                                    ++$indeks;
                                    ++$count;
                                }

                                echo '<table>';
                                    echo '<tr>';
                                        echo '<td>';
                                for ($j = 0; $j < $indeks; ++$j){
                                        //echo '&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp';
                                        echo '<div>';
                                        echo '<table>';
                                            echo '<tr>';
                                                echo '<td>';
                                                    echo '<a title="'.$pemilikArray3[$j].'" href="profile.php?un='.$pemilikArray3[$j].'"><img class="boxy2" src="'.$gambarArray3[$j].'" alt="fol_'.$j.'" width="50" height="50" name="foto_fol_'.$j.'" id="foto_fol_'.$j.'"/></a>';
                                                echo '</td>';
                                            echo '</tr>';
                                            echo '<tr>';
                                                echo '<td>';
                                                    if (strlen($pemilikArray3[$j]) > 6){
                                                        $pem3[$j] = substr($pemilikArray3[$j], 0, 4).'..' ;
                                                    }
                                                    else{
                                                        $pem3[$j] = $pemilikArray3[$j];
                                                    }
                                                    echo '<a href="profile.php?un='.$pemilikArray3[$j].'">'.$pem3[$j].'</a>';
                                                echo '</td>';
                                            echo '</tr>';
                                        echo '</table>';
                                        echo '</div>';

                                        echo '</td>';

                                        if (($j % 10) == 6){
                                            echo '</tr>';
                                            echo '<tr>';
                                        }

                                        echo '<td>';
                                }


                                        echo '</td>';
                                    echo '</tr>';
                                echo '</table>';

                                        echo '&nbsp';
                                    echo '</div>';
                                if ($count == 0){
                                    echo '<br/>';
                                    echo '<br/>';
                                    echo '<br/>';
                                    echo '<br/>';
                                    echo '<br/>&nbsp';
                                }
                            }

                            echo '</div>';
                            
                            
                        echo '</td>';
                    echo '</tr>';
                echo '</table>';
                echo '<div  class="button180" onclick="backToProfile(\''.$user.'\')">Back to Profile</div>';
                
?>